package B刷题.链表;

/**
 * @author wei.zhao
 * @description: 82.删除排序链表中的重复元素II
 * @date 2022/11/30 16:50
 */
public class Code06_DeleteDuplicates {

    public ListNode deleteDuplicates(ListNode head) {
        ListNode result = null;
        ListNode preNode = null;
        while (head != null) {
            int val = head.val;
            if (head.next != null && head.next.val == val) {
                if (preNode != null) {
                    preNode.next = null;
                }
                while (head.next != null && head.next.val == val) {
                    head = head.next;
                }
                head = head.next;
            } else {
                if (result == null) {
                    result = head;
                    preNode = head;
                } else {
                    preNode.next = head;
                    preNode = head;
                }
                head = head.next;
            }
        }
        return result;
    }

    public class ListNode {
        int val;
        ListNode next;
    }

}
